﻿#include "stdafx.h"
#include "ConvISO8859_1.h"

#define COUNTOF(a)	(sizeof(a)/sizeof(a[0]))

static LPCSTR lpEntityNames[] = {
	"quot",		"apos",		"amp",		"lt",		"gt",
	"nbsp",		"iexcl",	"cent",		"pound",	"curren",
	"yen",		"brvbar",	"sect",		"uml",		"copy",
	"ordf",		"laquo",	"not",		"shy",		"reg",
	"macr",		"deg",		"plusmn",	"sup2",		"sup3",
	"acute",	"micro",	"para",		"middot",	"cedil",
	"sup1",		"ordm",		"raquo",	"frac14",	"frac12",
	"frac34",	"iquest",	"times",	"divide",	"Agrave",
	"Aacute",	"Acirc",	"Atilde",	"Auml",		"Aring",
	"AElig",	"Ccedil",	"Egrave",	"Eacute",	"Ecirc",
	"Euml",		"Igrave",	"Iacute",	"Icirc",	"Iuml",
	"ETH",		"Ntilde",	"Ograve",	"Oacute",	"Ocirc",
	"Otilde",	"Ouml",		"Oslash",	"Ugrave",	"Uacute",
	"Ucirc",	"Uuml",		"Yacute",	"THORN",	"szlig",
	"agrave",	"aacute",	"acirc",	"atilde",	"auml",
	"aring",	"aelig",	"ccedil",	"egrave",	"eacute",
	"ecirc",	"euml",		"igrave",	"iacute",	"icirc",
	"iuml",		"eth",		"ntilde",	"ograve",	"oacute",
	"ocirc",	"otilde",	"ouml",		"oslash",	"ugrave",
	"uacute",	"ucirc",	"uuml",		"yacute",	"thorn",
	"yuml"
};

static size_t nEntityLens[] = {
	4,	4,	3,	2,	2,
	4,	5,	4,	5,	6,
	3,	6,	4,	3,	4,
	4,	5,	3,	3,	3,
	4,	3,	6,	4,	4,
	5,	5,	4,	6,	5,
	4,	4,	5,	6,	6,
	6,	6,	5,	6,	6,
	6,	5,	6,	4,	5,
	5,	6,	6,	6,	5,
	4,	6,	6,	5,	4,
	3,	6,	6,	6,	5,
	6,	4,	6,	6,	6,
	5,	4,	6,	5,	5,
	6,	6,	5,	6,	4,
	5,	5,	6,	6,	6,
	5,	4,	6,	6,	5,
	4,	3,	6,	6,	6,
	5,	6,	4,	6,	6,
	6,	5,	4,	6,	5,
	4
};

static BYTE tcEntityNumbers[] = {
	34,		39,		38,		60,		62,
	/*160 대신 32*/
	32,		161,	162,	163,	164,
	165,	166,	167,	168,	169,
	170,	171,	172,	173,	174,
	175,	176,	177,	178,	179,
	180,	181,	182,	183,	184,
	185,	186,	187,	188,	189,
	190,	191,	215,	247,	192,
	193,	194,	195,	196,	197,
	198,	199,	200,	201,	202,
	203,	204,	205,	206,	207,
	208,	209,	210,	211,	212,
	213,	214,	216,	217,	218,
	219,	220,	221,	222,	223,
	224,	225,	226,	227,	228,
	229,	230,	231,	232,	233,
	234,	235,	236,	237,	238,
	239,	240,	241,	242,	243,
	244,	245,	246,	248,	249,
	250,	251,	252,	253,	254,
	255
};

CConvISO8859_1::CConvISO8859_1(void)
{
}


CConvISO8859_1::~CConvISO8859_1(void)
{
}

bool CConvISO8859_1::convOne(LPCTSTR lp, bool bStrict, TCHAR &tc, int &len)
{
	if (*(lp++) != _TCHAR('&')) return false;

	// 비교순서:
	//  &*;	/ case-sensitive → bStrict==true면 이것만 실행. false이면 아예 필요 없음
	//  &*;	/ case-insensitive (첫 글자만 case sensitive)
	//  &*;	/ case-insensitive
	//  &*	/ case-insensitive

	if (bStrict) {
		// &*;	/ case-sensitive
		for (int i=0; i<COUNTOF(lpEntityNames); i++) {
			if ((tcschncmp(lp, lpEntityNames[i], nEntityLens[i])==0) && (lp[nEntityLens[i]]==_TCHAR(';'))) {
				tc = static_cast<TCHAR>(tcEntityNumbers[i]);
				len = nEntityLens[i]+2;
				return true;
			}
		}
	} else {
		// &*;	/ case-insensitive (첫 글자만 case sensitive)
		for (int i=0; i<COUNTOF(lpEntityNames); i++) {
			if ((tcschnicmp(lp, lpEntityNames[i], nEntityLens[i])==0) && (lp[0]==lpEntityNames[i][0]) && (lp[nEntityLens[i]]==_TCHAR(';'))) {
				tc = static_cast<TCHAR>(tcEntityNumbers[i]);
				len = nEntityLens[i]+2;
				return true;
			}
		}

		// &*;	/ case-insensitive
		for (int i=0; i<COUNTOF(lpEntityNames); i++) {
			if ((tcschnicmp(lp, lpEntityNames[i], nEntityLens[i])==0) && (lp[nEntityLens[i]]==_TCHAR(';'))) {
				tc = static_cast<TCHAR>(tcEntityNumbers[i]);
				len = nEntityLens[i]+2;
				return true;
			}
		}

		// &*	/ case-insensitive
		for (int i=0; i<COUNTOF(lpEntityNames); i++) {
			if ((tcschnicmp(lp, lpEntityNames[i], nEntityLens[i])==0) && (!_istalnum(lp[nEntityLens[i]]))) {
				tc = static_cast<TCHAR>(tcEntityNumbers[i]);
				len = nEntityLens[i]+1;
				return true;
			}
		}
	}

	return false;
}

bool CConvISO8859_1::convOne(LPCSTR lp, bool bStrict, TCHAR &tc, int &len)
{
	if (*(lp++) != '&') return false;

	// 비교순서:
	//  &*;	/ case-sensitive → bStrict==true면 이것만 실행. false이면 아예 필요 없음
	//  &*;	/ case-insensitive (첫 글자만 case sensitive)
	//  &*;	/ case-insensitive
	//  &*	/ case-insensitive

	if (bStrict) {
		// &*;	/ case-sensitive
		for (int i=0; i<COUNTOF(lpEntityNames); i++) {
			if ((strncmp(lp, lpEntityNames[i], nEntityLens[i])==0) && (lp[nEntityLens[i]]==_TCHAR(';'))) {
				tc = static_cast<TCHAR>(tcEntityNumbers[i]);
				len = nEntityLens[i]+2;
				return true;
			}
		}
	} else {
		// &*;	/ case-insensitive (첫 글자만 case sensitive)
		for (int i=0; i<COUNTOF(lpEntityNames); i++) {
			if ((_strnicmp(lp, lpEntityNames[i], nEntityLens[i])==0) && (lp[0]==lpEntityNames[i][0]) && (lp[nEntityLens[i]]==_TCHAR(';'))) {
				tc = static_cast<TCHAR>(tcEntityNumbers[i]);
				len = nEntityLens[i]+2;
				return true;
			}
		}

		// &*;	/ case-insensitive
		for (int i=0; i<COUNTOF(lpEntityNames); i++) {
			if ((_strnicmp(lp, lpEntityNames[i], nEntityLens[i])==0) && (lp[nEntityLens[i]]==_TCHAR(';'))) {
				tc = static_cast<TCHAR>(tcEntityNumbers[i]);
				len = nEntityLens[i]+2;
				return true;
			}
		}

		// &*	/ case-insensitive
		for (int i=0; i<COUNTOF(lpEntityNames); i++) {
			if ((_strnicmp(lp, lpEntityNames[i], nEntityLens[i])==0) && (!_istalnum(lp[nEntityLens[i]]))) {
				tc = static_cast<TCHAR>(tcEntityNumbers[i]);
				len = nEntityLens[i]+1;
				return true;
			}
		}
	}

	return false;
}
